﻿/****************************************************************************************************
 JavaScript Document
 加载xml文档
 可以在IE5+, FireFox, Chrome, Opera, Safari浏览器运行
 
 使用方法：
 	将代表省、市、区的select的id以数组的形式给xn.pcc()方法[如:xn.pcc(['province','city','county'])].
 实例：
 	<html>
	<head>
	<title>PCC省市区级联</title>
	<script type="text/javascript" src="prcico.js"></script>
	<script type="text/javascript">
		xn.init(['province','city','county']);
	</script>
	</head>
	<body>
		<select id="province"></select><select id="city"></select><select id="county"></select>
	</body>
	</html>
 
  修改了在Chrome加载不成功的问题
 /***************************************************************************************************/

var xn=new Object();
xn.init=function(a){
	var xmldoc;
	if (window.XMLHttpRequest){// code for IE7+, FireFox, Chrome, Opera, Safari
		xmldoc = new XMLHttpRequest();
	}else {	// code for IE6, IE5
		xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmldoc.onreadystatechange=function(){
		if (xmldoc.readyState == 4){
			if( xmldoc.status == 200){
				d = xmldoc.responseXML;
				getCascade(a,d);
			}else{
				xn.init(a);
			}
		}
	};
	xmldoc.open("GET","/WORKDIR/20111031.xml",true);
	xmldoc.send();
};

function getCascade(a,d){
	var province=document.getElementById(a[0]);
	var city=document.getElementById(a[1]);
	var county=document.getElementById(a[2]);
	//初始化级联框
	var pro=d.getElementsByTagName("province");
	if(a[7]=='containHeader'){
		var oOption1 = new Option("省份","省份",true,true);
		province.options.add(oOption1);
		if(a[4] == "省份"){
			var oOption2 = new Option("城市","城市",true,true);
			city.options.add(oOption2);
			var oOption3 = new Option("区域","区域",true,true);
			county.options.add(oOption3);
		}
	}
	for(i=0;i<pro.length;i++){
		var oOption;
		if(pro[i].getAttribute(a[3])==a[4]){
			oOption =new Option(pro[i].getAttribute('name'),pro[i].getAttribute(a[3]),true,true);
			provinceExist = true;
		} else {
			oOption =new Option(pro[i].getAttribute('name'),pro[i].getAttribute(a[3]));
		}
		province.options.add(oOption);
		if(province.value==pro[i].getAttribute(a[3])){
			var cit=pro[i].getElementsByTagName("city");
			for(j=0;j<cit.length;j++){
				var oOption;
				if(cit[j].getAttribute(a[3])==a[5]){//选中
					oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute(a[3]),true,true);
				}else {
					oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute(a[3]));
				}
				city.options.add(oOption);
				if(city.value==cit[j].getAttribute(a[3])){
					var cou=cit[j].getElementsByTagName('county');
					for(k=0;k<cou.length;k++){
						var oOption;
						if(cou[k].getAttribute(a[3])==a[6]){
							oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute(a[3]),true,true);
						} else {
							oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute(a[3]));
						}
						county.options.add(oOption);
					}
				}
			}
		}
	}
	//省菜单onchange
	province.onchange=function(){
		city.options.length=null;
		county.options.length=null;
		for(i=0;i<pro.length;i++){
			if(province.value==pro[i].getAttribute(a[3])){
				var cit=pro[i].getElementsByTagName("city");
				for(j=0;j<cit.length;j++){
					var oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute(a[3]));
					city.options.add(oOption);
					if(city.value==cit[j].getAttribute(a[3])){
						var cou=cit[j].getElementsByTagName('county');
						for(k=0;k<cou.length;k++){
							var oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute(a[3]));
							county.options.add(oOption);
						}
					}
				}
			}
		}
		if(a[7]=='containHeader' && province.value=='省份') {
			var oOption2 = new Option("城市","城市");
			city.options.add(oOption2);
			var oOption3 = new Option("区域","区域");
			county.options.add(oOption3);
		}
		initAddress();
	};
	//市菜单onchange
	city.onchange=function(){
		var cityval=city.value;
		city.options.length=null;
		for(i=0;i<pro.length;i++){
			if(province.value==pro[i].getAttribute(a[3])){
				var cit=pro[i].getElementsByTagName("city");
				for(j=0;j<cit.length;j++){
					var oOption=new Option(cit[j].getAttribute('name'),cit[j].getAttribute(a[3]));
					city.options.add(oOption);
					city.value=cityval;
					if(city.value==cit[j].getAttribute(a[3])){
						county.options.length=null;
						var cou=cit[j].getElementsByTagName('county');
						for(k=0;k<cou.length;k++){
							var oOption=new Option(cou[k].getAttribute('name'),cou[k].getAttribute(a[3]));
							county.options.add(oOption);
						}
					}
				}
			}
		}
		initAddress();
	};
}
